const path = require('path');
const fs = require('fs');
const {exec} = require('child_process');
const {Controller} = require('ee-core');
const {app: electronApp, shell} = require('electron');
const dayjs = require('dayjs');
const Ps = require('ee-core/ps');
const Log = require('ee-core/log');
const Services = require('ee-core/services');
const Conf = require('ee-core/config');
const Addon = require('ee-core/addon');
const EE = require('ee-core/ee');

/**
 * electron-egg framework - 功能demo
 * @class
 */
class FrameworkController extends Controller {

    constructor(ctx) {
        super(ctx);
    }


    async checkTable(args) {
        const {action, tabName} = args;
        const data = {
            action,
            result: null,
            all_list: [],
            code: 0
        };
        try {
            // test
            Services.get('database.sqlitedb').getDataDir();
        } catch (err) {
            console.log(err);
            data.code = -1;
            return data;
        }

        data.result = await Services.get('database.spiderdb').checkAndCreateTableSqlite(tabName);

    }

    async saveSpiderConfig(args){
        const {tableName, json} = args;

        const data = {
            result: null,
            all_list: [],
            code: 0
        };
        const now = dayjs().format('YYYY-MM-DD HH:mm:ss')
        const param = {
            Json: json,
            CreateTime: now,
            UpdateTime:now
        };
        data.result = await Services.get('database.spiderdb').addConfig(param);
        return data;
    }

    async getLastConfig(args){
        const {tableName, json} = args;

        const data = {
            result: null,
            all_list: [],
            code: 0
        };
        data.result = await Services.get('database.spiderdb').getLastConfig();
        return data;
    }
}

FrameworkController.toString = () => '[class FrameworkController]';
module.exports = FrameworkController;